

from fastapi import APIRouter, HTTPException

from config.common_config import DATABASE_CONFIG
from database.embedding_model import EmbeddingModel
from models.request_models import SearchRequest, EncryptionRequest, CommonResponse
from models.request_models import SearchResponse
from server.chat_record_server import ChatRecordServer
from workflows.chatFlow import ChatFlow

from utils.aes_util_no_salt import AES256Cipher

router = APIRouter(prefix="/secret/api/v1", tags=["chat"])

# 初始化组件
chat_record_server = ChatRecordServer(
    host=DATABASE_CONFIG["HOST"],
    port=DATABASE_CONFIG["PORT"],
    user=DATABASE_CONFIG["USER"],
    pwd=DATABASE_CONFIG["PASSWORD"]
)
embedding_model = EmbeddingModel()
import logging

chatFlow = ChatFlow()

logger = logging.getLogger(__name__)

@router.post("/encryption", response_model=CommonResponse)
async def search_chat_history(request: EncryptionRequest):
    #根据saasId获取密钥
    saasId=request.saasId
    if not saasId:
        return CommonResponse(status="500",msg="saasId不能为空")
    pwd=""
    if(saasId.__eq__("1")):
        pwd="szsk123"
    else:
        pwd="szsk123"
    #加密
    key = AES256Cipher.derive_key(pwd)
    cipher = AES256Cipher(key)

    secretss=list()

    if request.strs.__len__()>0:

        for str in request.strs:
            secretss.append(cipher.encrypt_cbc(str))
    return CommonResponse(data=secretss)